<template>
    <div style="margin: 5px 10px 0px 10px">
        <el-row :gutter="20">
            <!--用户数据-->
            <el-col :span="24" :xs="24">
                <el-form ref="queryParamsRef" :inline="true" :model="queryParams" class="search-form config-form"
                    label-width="80px">
                    <el-form-item v-if="!userInfo.orgId" label="所属机构" prop="orgId">
                        <select-org-tree :value="queryParams.orgId" @orgNodeClick="orgNodeClick" />
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="手术字典类型" prop="operationType">
                <el-radio-group v-model="form.operationType" style="margin-top: 5px">
                    <el-radio v-for="(item, index) in dictDataMap['operation_code_dict']" :key="index"
                              :value="item.dictValue" class="checkbox-item">
                        {{ item.dictLabel }}
                    </el-radio>
                </el-radio-group>
            </el-form-item>

            <el-divider></el-divider>

            <el-form-item label="诊断类型" prop="diagnosisType">
                <el-radio-group v-model="form.diagnosisType" style="margin-top: 5px">
                    <el-radio v-for="(item, index) in dictDataMap['diagnosis_code_dict']" :key="index"
                              :value="item.dictValue" class="checkbox-item">
                        {{ item.dictLabel }}
                    </el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
    </div>
</template>

<script lang="ts" setup>
import {addOperationDiagnosisConfig, listOperationDiagnosisConfig} from '@/api/base/operationDiagnosisConfig'
// import { listSysOrg } from "@/api/system/sysOrg";
import {useUserStoreHook} from "@/store/modules/user";
import {useDictStoreHook} from "@/store/modules/dict";

const useUserStore = useUserStoreHook();
const { proxy } = getCurrentInstance() as any
const dictStore = useDictStoreHook();
const dictDataMap = ref(dictStore.isTypeGetData('operation_code_dict', 'diagnosis_code_dict'))
const formRef = ref()
const emits = defineEmits(['modifyLoading'])
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo

// 手术类型集合
const operationList = ref([])
const queryParams = ref<any>({
    orgId: ''
})
const form = ref<any>({
    // 选中的手术类型
    operationType: '',
    // 诊断选中类型
    diagnosisType: '',
    orgId: ''
})
const rules = ref({
    operationType: [
        { required: true, message: "手术字典类型不能为空", trigger: "change" }
    ],
    diagnosisType: [
        { required: true, message: "诊断类型不能为空", trigger: "change" }
    ],
})
if (userInfo.orgId) {
    queryParams.value.orgId = userInfo.orgId
    listOperationDiagnosisConfigFun()
}
// 机构树点击事件
function orgNodeClick(data, _e) {
    queryParams.value.orgId = data.orgId
    if (queryParams.value.orgId) {
        listOperationDiagnosisConfigFun()
    }
}
/** 查询依循在的配置信息 */
function listOperationDiagnosisConfigFun() {
    listOperationDiagnosisConfig({ "orgId": queryParams.value.orgId }).then((res: any) => {
        const data = res.data
        if (data.length > 0) {
            form.value.diagnosisType = data[0].diagnosisType
            form.value.operationType = data[0].operationType
        } else {
            form.value.diagnosisType = ''
            form.value.operationType = ''
        }
    })
}
/** 提交按钮 */
function submitForm() {
    if (!queryParams.value.orgId) {
        return ElMessage.error("请选择所属机构再操作")
    }
    (formRef.value as any).validate(valid => {
        if (valid) {
            ElMessageBox.confirm('确定需要保存数据吗？',

                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            ).then(() => {
                form.value.orgId = queryParams.value.orgId
                emits("modifyLoading", true)
                addOperationDiagnosisConfig(form.value).then(() => {
                    emits("modifyLoading", false)
                    ElMessage.success("诊断类型、手术字典类型配置成功")
                    listOperationDiagnosisConfigFun()
                })
            }).catch(() => {
                emits("modifyLoading", false)
            })
        }
    })
}

defineExpose({
    submitForm
})
</script>

<script lang="ts">
export default {
    name: "operAndDiagnosisConfig"
}
</script>

<style scoped>
.checkbox-item {
    width: 260px;
    margin-bottom: 10px
}
</style>
